package node;

import java.util.ArrayList;
import java.util.List;

import utils.Base;
import utils.titles.Title;

import com.google.common.base.Joiner;

/**
 * Created by thormentor on 29-04-2014.
 */
public abstract class Node extends Base {

	public enum Display{
		PRETTY, INLINE
	}

	private final List<Node> children = new ArrayList<>();
	private Node parent;
	private Display display;

	protected Node() {
		super(Title.SNAKE_CASE);
	}

	@Override
	public String toString() {
		if (display == Display.INLINE)
			return getIndentation() + getBegin() + getContent() + getEnd();

		return Joiner.on("\n").skipNulls().join(
				getIndentation() + getBegin(),
				getContent(),
				children.size() == 0 ? null : getIndentation() + getEnd()
				);
	}

	protected abstract String getBegin();

	protected final String getContent() {
		return children.size() == 0 ? null : Joiner.on("\n").join(children);
	}

	protected boolean isClosed() {
		return children.size() == 0;
	}

	protected abstract String getEnd();

	public String getIndentation() {
		return parent == null || parent.display == Display.INLINE ? "" : parent.getIndentation() + "\t";
	}

	public Node appendChild(final Node node) {
		children.add(node);
		node.setParent(this);
		return this;
	}

	public Node appendChildren(final Node... nodes){
		for(final Node node : nodes)
			appendChild(node);
		return this;
	}


	public void setParent(final Node parent) {
		this.parent = parent;
	}

	public void setDisplay(final Display display) {
		this.display = display;
	}
}
